package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class Update extends JInternalFrame {
	private JLabel label;
	private JTextField codeTF;
	private JButton bnt1;
	private JLabel lblEa;
	private JTextField eaTF;
	private JLabel kekka;
	private JButton btn2;
	private JLabel lblNewLabel;
	private JTextField serialTF;
	private JButton btnNewButton;
	
	Connection con;
	int serial;
	DatabaseTest main;
	
	public Update(Connection con,DatabaseTest main) {
		super("수정",true,true,true,true);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosing(InternalFrameEvent e) {
				serialTF.setText("");
				codeTF.setText("");
				eaTF.setText("");
			}
		});
		this.con=con;
		this.main=main;
		setBounds(200, 200, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getCodeTF());
		getContentPane().add(getBnt1());
		getContentPane().add(getLblEa());
		getContentPane().add(getEaTF());
		getContentPane().add(getKekka());
		getContentPane().add(getBtn2());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getSerialTF());
		getContentPane().add(getBtnNewButton());

	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("code");
			label.setBounds(39, 82, 57, 15);
		}
		return label;
	}
	private JTextField getCodeTF() {
		if (codeTF == null) {
			codeTF = new JTextField();
			codeTF.setBounds(133, 79, 116, 21);
			codeTF.setColumns(10);
		}
		return codeTF;
	}
	private JButton getBnt1() {
		if (bnt1 == null) {
			bnt1 = new JButton("조회");
			bnt1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						int serial = Integer.parseInt(serialTF.getText());
						String sql = "select * from ptbl where serial=?";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setInt(1, serial);
						ResultSet rs = psmt.executeQuery();
						if(rs.next()){
							codeTF.setText(rs.getString(1));
							eaTF.setText(rs.getInt(2)+"");
							kekka.setText("serial번호 "+serial+"의 데이타가 조회되었습니다.");
						}else{
							kekka.setText("데이타가 없습니다");
							codeTF.setText("");
							eaTF.setText("");
							serialTF.requestFocus();
						}
						rs.close();
						psmt.close();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			bnt1.setBounds(273, 33, 97, 23);
		}
		return bnt1;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("ea");
			lblEa.setBounds(39, 130, 57, 15);
		}
		return lblEa;
	}
	private JTextField getEaTF() {
		if (eaTF == null) {
			eaTF = new JTextField();
			eaTF.setBounds(133, 127, 116, 21);
			eaTF.setColumns(10);
		}
		return eaTF;
	}
	private JLabel getKekka() {
		if (kekka == null) {
			kekka = new JLabel("New label");
			kekka.setBounds(38, 194, 332, 23);
		}
		return kekka;
	}
	private JButton getBtn2() {
		if (btn2 == null) {
			btn2 = new JButton("수정");
			btn2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						serial = Integer.parseInt(serialTF.getText());
						int ea = Integer.parseInt(eaTF.getText());
						String c = codeTF.getText();
						
						String sql = "update ptbl set code=?, ea=? where serial=?";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setString(1, c);
						psmt.setInt(2, ea);
						psmt.setInt(3, serial);
						psmt.executeUpdate();
						kekka.setText("serial 번호"+serial+"의 데이타가 수정되었습니다");
						eaTF.setText("");
						codeTF.setText("");
						
						if(main.select != null){
							main.select.setList();
						}
						
						psmt.close();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			btn2.setBounds(273, 126, 97, 23);
		}
		return btn2;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("serial");
			lblNewLabel.setBounds(39, 37, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getSerialTF() {
		if (serialTF == null) {
			serialTF = new JTextField();
			serialTF.setBounds(133, 34, 116, 21);
			serialTF.setColumns(10);
		}
		return serialTF;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("삭제");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						serial = Integer.parseInt(serialTF.getText());
						
						String sql = "delete from ptbl where serial=?";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setInt(1, serial);
						int r = psmt.executeUpdate();
						if(r==1){
							kekka.setText("serial 번호 "+serial+"의 데이타가 삭제되었습니다");
						}else{
							kekka.setText("삭제할 데이타가 없습니다");
						}
						codeTF.setText("");
						eaTF.setText("");
						if(main.select != null){
							main.select.setList();
						}
						psmt.close();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(273, 159, 97, 23);
		}
		return btnNewButton;
	}
	
	public void setData(String s, String c, String e){
		serialTF.setText(s);
		codeTF.setText(c);
		eaTF.setText(e);
	}
}
